package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.redis.RedisService.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.redis.RedisService.IUserInfoRedisService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("users")
//DestinationsController
public class UserInfoController {
    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appkey}")
    private String appkey;

    //注入用户服务接口
    @Autowired
    private IUserInfoService userInfoService;

    //注入用户缓存服务接口
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    //注入攻略缓存服务接口
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    //获取某个用户的信息
    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    //判断用户是否注册
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        /**
         * 判断是否存在
         * 如果存在就返回true
         * 如果不存在就返回false
         */
        boolean exist = userInfoService.checkPhone(phone);

        /**
         * 如果存在就要返回false不能注册
         * 如果不存在就返回true可以注册
         * 所以要取反
         */
        return !exist;
    }

    //发送验证码
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone, smsUrl, appkey);
        return JsonResult.success();
    }

    //注册登录
    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }
    //登录
    @PostMapping("/login")
    public Object login(String username, String password) {
        //用户登录
        UserInfo user = userInfoService.login(username, password);
        //将用户设置到redis中
        String token = userInfoRedisService.setToken(user);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", user);
        return JsonResult.success(map);
    }
    @RequireLogin//访问这个方法必须登录才可以
    //获取当前用户信息
    @GetMapping("/getCurrentUser")
    public Object getCurrentUser(HttpServletRequest request) {
        String token = request.getHeader("token");
       UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }

    //收藏数据添加
    @GetMapping ("/strategies/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo) {
        List<String> favor = strategyStatisVORedisService.getFavor(sid, userInfo.getId());
        return JsonResult.success(favor);
    }

}
